var utils = require("../../../utils/util.js");
var wxcache = require("../../../utils/wxcache.js");

var api = require('../../../config/api.js');
var user = require('../../../services/user.js');
var app = getApp();

 
Page({
  data: {
    loading: false,
    hasMore: true,
    disableBtn: false,
    user: {},
    userInfo: {},
    maxPersonalSignatureInput:20,

    common_memo:'点击设置',
    /**
    personWidth: '100%',
    inputWidth:'-70rpx',
    imgWidth: '-120rpx',
    */
    uHeadUrl:'',
    uNickName: '',
    uCityName: '',
    uCityName_memo:'正在获取当前位置...',
    uPhonenumber: '',
    uPersonalSignature: '',
    uPersonalSignature_memo: '写下你想对世界说的话，就现在！',
    uLikeSpot:'',
    uLikeSpot_memo:'点击输入喜欢的景点',
    uWentCity:'',
    uWentCity_memo: '点击输入去过的城市',
    uGoodAt:'',
    uGoodAt_memo: '点击输入拿手的事',
    uBirthday:'',
    uBirthday_memo:'点击设置生日',
    uGender:'',
    uGender_memo:'点击设置性别',
    uIsSingle:'',
    uIsSingle_memo:'点击设置是否单身',
    uMakeFriends:'',
    uMakeFriends_memo:'点击设置是否交友',
    uConstellation:'',
    uConstellation_memo:'设置生日可推算星座喔~',
    uLove:''
  },
  onLoad: function (options) {

     this.queryInfo();
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
 
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  queryInfo() {

    let that = this;
    var phonenumber = wxcache.get("phoneNumber");
    var userinfo = wxcache.get("userInfo");

    utils.request(api.QueryUserInfo, { mobile: phonenumber }).then(function (res) {

      console.log(res.responseBody)
      let user = res.responseBody
      that.setData({
      
        uHeadUrl: user.headUrl == '' || user.headUrl == undefined ? userinfo.headUrl : user.headUrl,
        uNickName: user.nickName ? user.nickName : '',
        uCityName: user.currentLocation ? user.currentLocation : '',
        uPhonenumber: user.mobile ? user.mobile : '',
        uPersonalSignature: user.personalSignature ? user.personalSignature : '',
        uLikeSpot: user.likeSpot ? user.likeSpot :'',
        uWentCity: user.wentCity ? user.wentCity :'',
        uGoodAt: user.goodAt ? user.goodAt : '',
        uBirthday: user.birthday ? user.birthday :'',
        uGender: user.gender ? user.gender : '',
        uIsSingle: user.isSingle ? user.isSingle : '',
        uMakeFriends: user.makeFriends ? user.makeFriends :'',
        uConstellation: user.constellation ? user.constellation : '', 
        uLove: user.love ? user.love :''
      })
      if (user.birthday){
        utils.request(api.Calconstellation, { birthday: user.birthday }).then(res => {
          console.log(res)
          that.setData({
            uConstellation: res.responseBody
          })
        })
      }
      

      if (!user.currentLocation){
        that.getLocation()
      }


      if (user.gender == 'M') {
        that.setData({
          uGender: '男',
        });
      } else if (user.gender == 'F') {
        that.setData({
          uGender: '女',
        });
      }
      if (user.isSingle == 1) {
        that.setData({
          uIsSingle: '单身',
        });
      } else if (user.isSingle == 0) {
        that.setData({
          uIsSingle: '非单身',
        });
      }
      if (user.makeFriends == 1) {
        that.setData({
          uMakeFriends: '交友',
        });
      } else if (user.makeFriends == 0) {
        that.setData({
          uMakeFriends: '不交友',
        });
      }
    })



  },

  bind_headtap: function(){

      wx.showModal({
        title: '',
        content: '头像暂时不支持修改喔~',
        showCancel:false
      })
  },

  // //获取经纬度方法
  getLocation: function () {
    var that = this
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        console.log("lat:" + latitude + " lon:" + longitude);
        that.getCity(latitude, longitude);//当地
      }
    })
  },

  // //获取城市信息
  getCity: function (lap, lop) {
    var that = this
    var url = api.DecodeCityFromLaLo;
    var params = {
      ak: app.globalData.ak,
      output: "json",
      location: lap + "," + lop
    }
    wx.request({
      url: url,
      data: params,
      success: function (res) {
        var city = res.data.result.addressComponent.city;
        var district = res.data.result.addressComponent.district;
        var street = res.data.result.addressComponent.street;

        var currentCity = city + district + street
        console.log(currentCity)
        that.setData({
          uCityName: currentCity
        })

      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  phonetap:function(e){

    wx.showModal({
      content: '手机号不能修改喔~',
      showCancel:false
    })

  },
  constellationTap : function(e){
    wx.showModal({
      content: '设置生日才能推算星座喔~',
      showCancel: false
    })
  },

  // bindinput----------------------------

  nicknameText: function (e) {
    this.setData({
      uNickName: e.detail.value
    });
  },
  cityText: function (e) {
    this.setData({
      uCityName: e.detail.value
    });
  },
  personalSignatureText: function (e) {

    // 获取输入框的内容
    var value = e.detail.value;

    // 获取输入框内容的长度
    var len = parseInt(value.length);
    console.log('当前输入长度：' + len)
    
    //最多字数限制
    if (len >= this.data.maxPersonalSignatureInput){
      wx.showModal({
        content: '最多只能输入20字喔~',
        showCancel:false
      })
    }

    //设置签名宽度
    /**
    if (len == 0){
      this.setData({
        personWidth: "100%",
        inputWidth:'-70rpx',
        imgWidth:'-120rpx'
      });
    }else{
      var w = len * 4 + 20;
      w = w + '%';
      var i = 0 + "rpx";
      this.setData({
        personWidth: w,
        inputWidth: '0rpx',
        imgWidth:i
      });
    }
    */
    this.setData({
      uPersonalSignature: e.detail.value
    });
  },
  likeSpotText: function (e) {
    this.setData({
      uLikeSpot: e.detail.value
    });
  },
  wentCityText: function (e) {
    this.setData({
      uWentCity: e.detail.value
    });
  },
  goodAtText: function (e) {
    this.setData({
      uGoodAt: e.detail.value
    });
  },
  birthdayText: function (e) {
    this.setData({
      uBirthday: e.detail.value
    });
  },
  sexText: function (e) {
    this.setData({
      uGender: e.detail.value
    });
  },
  isSingleText: function (e) {
    this.setData({
      uIsSingle: e.detail.value
    });
  },
  makeFriendsText: function (e) {
    this.setData({
      uMakeFriends: e.detail.value
    });
  },
  constellationText: function (e) {
    this.setData({
      uConstellation: e.detail.value
    });
  },

  loveText: function (e) {
    this.setData({
      uLove: e.detail.value
    });
  },
  //--------------------------------------------------


  //  点击日期组件确定事件  
  bindDateChange: function (e) {

    let that =this;
    console.log(e.detail.value)
    this.setData({
      uBirthday: e.detail.value,
    })

    utils.request(api.Calconstellation, { birthday: e.detail.value}).then(res=>{
      console.log(res)
      that.setData({
        uConstellation :  res.responseBody
      })
    })

  },
  genderText: function (e) {
    this.setData({
      uGender: e.detail.value
    });
  },
  genderBin: function () {
    var that = this;
    wx.showActionSheet({
      itemList: ['男', '女'],
      success: function (res) {
        console.log(res.tapIndex);
        if (res.tapIndex == 0) {
          that.setData({
            uGender: '男',
          });
        } else if (res.tapIndex == 1) {
          that.setData({
            uGender: '女',
          });
        }
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  },

  isSingleBin: function () {
    var that = this;
    wx.showActionSheet({
      itemList: ['单身', '非单身'],
      success: function (res) {
        console.log(res.tapIndex);
        if (res.tapIndex == 0) {
          that.setData({
            uIsSingle: '单身',
          });
        } else if (res.tapIndex == 1) {
          that.setData({
            uIsSingle: '非单身',
          });
        }
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  },

  makeFriendsBin: function () {
    var that = this;
    wx.showActionSheet({
      itemList: ['交友', '不交友'],
      success: function (res) {
        console.log(res.tapIndex);
        if (res.tapIndex == 0) {
          that.setData({
            uMakeFriends: '交友',
          });
        } else if (res.tapIndex == 1) {
          that.setData({
            uMakeFriends: '不交友',
          });
        }
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  },


  typeText: function (e) {
    this.setData({
      uType: e.detail.value
    });
  },
  levelText: function (e) {
    this.setData({
      uLevel: e.detail.value
    });
  },
 
  /*保存提交*/
  updateAndPushUserInfo: function () {
    let that = this;

    var data = that.data
    var user={}
    user.mobile = data.uPhonenumber
    user.nickName =  data.uNickName;
    user.currentLocation =  data.uCityName
    user.personalSignature =  data.uPersonalSignature
      user.likeSpot =  data.uLikeSpot
      user.wentCity =  data.uWentCity
      user.goodAt =  data.uGoodAt  
      user.birthday =  data.uBirthday
      user.gender =  data.uGender =='女' ? 2 : 1
      user.isSingle =  data.uIsSingle == '单身' ? 1 : 0
      user.makeFriends =  data.uMakeFriends == '交友' ? 1 : 0
      user.constellation =  data.uConstellation
      user.love =  data.uLove


      //修改信息
      utils.request(api.InitUserInfo, user, 'POST').then(function (res) {

        if (res.code == 200) {

          console.log("修改成功")


          wx.switchTab({
            url: '/pages/ucenter/index/index',
          })

          wx.showToast({
            title: '修改成功',
            icon: 'success'
          })

        } else {
          console.log("资料修改失败，请联系客服")
          //提示验证码无效
          wx.showModal({
            title: '提示',
            content: '资料修改失败，请联系客服',
            showCancel: false
          });
        }

      })
  },
  controlOps(status) {
    if (status === 'enable') {
      this.setData({
        loading: false,
        disableBtn: false
      })
    } else if (status === 'disable') {
      this.setData({
        loading: true,
        disableBtn: true
      })
    } else {
      return;
    }
  },
  confirmModal(e) {
    this.setData({
      modalHidden: true,
      errorMessage: ''
    })
  }
})